package com.advertising.biz;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import com.advertising.dao.ReleaseDAO;
import com.advertising.model.Approve;
import com.advertising.model.CloneRelease;
import com.advertising.model.Release;
import com.advertising.permissions.dao.UsersDao;
import com.advertising.permissions.model.Users;
import com.tgyt.common.tools.page.Pagination;
import com.tgyt.common.tools.util.DictionaryMemoryUtils;
import com.tgyt.framework.dao.hspring.DAOInterface;
import com.tgyt.framework.service.BaseService;
@Service
public class ReleaseService extends BaseService<Release> implements
		IReleaseService {
	@Resource(name="usersDAO")
	private UsersDao usersDAO;
	
	@Resource(name="releaseDAO")
	private ReleaseDAO releaseDAO;
	@Override
	protected DAOInterface<Release> getDAO() {
	 
		return releaseDAO;
	}
	public int getMaxOrder(){
		return this.releaseDAO.getMaxOrder();
	}
	
	public List<Release> getAll(){
		return this.releaseDAO.getAll();
	}
	/**
	 * @Title: getPageList
	 * @Description: 具体某页显示多少条记录
	 * @param @param user 一条具体的用户信息
	 * @param @param page 当前页
	 * @param @param rows 一页显示多少条记录
	 * @param @return
	 * @return Pagination
	 * @throws
	 */
	public Pagination getPageList(Release release, int page, int rows,String sort,String order){
		return 	this.releaseDAO.getPageList(release, page, rows, sort, order);
	}
	@Override
	public List<CloneRelease> convert(List<Release> release) throws Exception {
		List<CloneRelease> clones = new ArrayList<CloneRelease>();
		/* DictionaryMemoryUtils dictionaryUtils = DictionaryMemoryUtils.getInstance();
		    Map maps = (Map)dictionaryUtils.getDictionarysMap().get("advertype");*/
		    
		if(release!=null && release.size()!=0){
			for(Iterator<Release> iter=release.iterator();iter.hasNext();){
				Release res = iter.next();
				CloneRelease c = new CloneRelease();
				BeanUtils.copyProperties(c, res);
				if(res.getUser()!=null){
					c.setUserid(res.getUser().getId()+"");
					c.setAdvername(res.getUser().getAdvername());
					c.setInternaluri(res.getUser().getInternaluri());
					c.setExternaluri(res.getUser().getExternaluri());
					c.setAddress(res.getUser().getAddress());
					c.setPidouid(res.getUser().getPidouid());
					c.setPhone(res.getUser().getPhone());
					c.setAdvertype(c.getAdvertype());
					c.setCoordinate(res.getLongitude()+" , "+res.getLatitude());
					
				}
				if (res.getApprove()!=null) {
					c.setClickprice(res.getApprove().getClickprice());//单次点击金额
				}
				
				 
				clones.add(c);
			}
		}
		return clones;
		
	}
	@Override
	public Release convert(CloneRelease cloneRelease) throws Exception {
		 if(cloneRelease==null){
			 return null;
		 }
		    Release  release = new Release();
		    BeanUtils.copyProperties(release,cloneRelease);
			Users user=(Users)SecurityUtils.getSubject().getSession().getAttribute("loginuser");
			release.setUser(user);
			Approve approve = new Approve();
		    if(cloneRelease.getApproveId()!=null){
		    	approve.setId(cloneRelease.getApproveId());
		    }
		    if (cloneRelease.getClickprice()!=null) {
		    	approve.setClickprice(cloneRelease.getClickprice());
			}
		    release.setApprove(approve);
		return release;
	}
	@Override
	public String findImageUrl(String userid) {
		
		return releaseDAO.findImageUrl(userid);
	}

}
